//
//  WSOrderDetailDataResolveTool.m
//  FD
//
//  Created by xiaofang wang on 2018/11/1.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "WSOrderDetailDataResolveTool.h"
#import "WSOrderCanPinMsgModel.h"
#import "WSOrderMsgModel.h"    //title content

//新
#import "WSOrderSectionModel.h"
#import "WSOrderCellModel.h"

@implementation WSOrderDetailDataResolveTool

//解析有用信息model
+ (WSOrderBigModel *)resolvingOrderBigModelWithDic:(NSDictionary *)dic {
    //单独摘取有用数据
    NSNumber *totalAmount = dic[@"totalAmount"];//1.总价
    NSNumber *couponAmount = dic[@"couponAmount"];//2.优惠金额
    NSNumber *freightAmount = dic[@"freightAmount"];//3.运费
    NSNumber *bonusAmount = dic[@"bonusAmount"]; //4.新增积分
    NSNumber *actualAmount = dic[@"actualAmount"]; //5.实际付款金额
    NSString *orderId = dic[@"id"]; //6.订单号
    
    //用户经纬度
    NSNumber *longitude = dic[@"userShippingAddress"][@"longitude"]; //7.经度
    NSNumber *latitude = dic[@"userShippingAddress"][@"latitude"];
    //2019-2-14可能为空,防止奔溃。
    latitude = latitude ? :@(0.0000);
    longitude = longitude ? :@(0.0000);
    
    //2019-2-18用户收货地址(用于大头针显示)
    NSString *detailStr = dic[@"userShippingAddress"][@"detail"];
    detailStr = detailStr ? :@""; //可有可无 防止为空导致的闪退
    
    NSNumber *phone = dic[@"userShippingAddress"][@"phone"];//9.用户的电话
    //2019-2-14可能为空,防止奔溃。
    phone = phone ? :@(000000);

    
    NSNumber *status= dic[@"status"]; //订单转态字典，用于判断订单详情接口请求参数、
    
    NSString *shopName = dic[@"shop"][@"name"]; //11.店铺名字
    NSString *shopLng = dic[@"shop"][@"longitude"];  //12-1商家经度
    NSString *shopLat = dic[@"shop"][@"latitude"];//12-2商家纬度
    
    //支付方式由于判断是在线支付:0; 还是线下支付:1
    NSNumber *paymentType = dic[@"paymentType"];
    
    //商家电话,联系商家的时候用
    NSString *shopPhone = dic[@"shop"][@"phone"];
   
    
    
    
    
    
    //组装字典 & 转模型
    //2019-2-14这里有炸弹💣,某一个字段可能为空。
//    FDLog(@"1111111111 Strat");
//    FDLog(@"totalAmount:%@",totalAmount);
//    FDLog(@"couponAmount:%@",couponAmount);
//    FDLog(@"freightAmount:%@",freightAmount);
//    FDLog(@"bonusAmount:%@",bonusAmount);
//    FDLog(@"orderId:%@",orderId);
//    
//    FDLog(@"longitude:%@",longitude); //可能为 null
//    FDLog(@"latitude:%@",latitude);   //可能为 null
//    FDLog(@"phone:%@",phone);  //可能为 null
//    
//    FDLog(@"status:%@",status);
//    FDLog(@"paymentType:%@",paymentType);
//    FDLog(@"shopPhone:%@",shopPhone);
//    FDLog(@"shopName:%@",shopName);
//    FDLog(@"shopLat:%@",shopLat);
//    FDLog(@"shopLng:%@",shopLng);
//    FDLog(@"1111111111 End");
    
    
    
    NSDictionary *bigDic = @{@"totalAmount":totalAmount,
                             @"couponAmount":couponAmount,
                             @"freightAmount":freightAmount,
                             @"bonusAmount":bonusAmount,
                             @"actualAmount":actualAmount,
                             @"orderId":orderId,
                             @"longitude":longitude,
                             @"latitude":latitude,
                             @"phone":phone,
                             @"status":status,
                             @"paymentType":paymentType,
                             @"shopPhone":shopPhone,
                             @"shopName":shopName,
                             @"shopLat":shopLat,
                             @"shopLng":shopLng,
                             @"detail":detailStr
                             };
    
    WSOrderBigModel *model = [WSOrderBigModel mj_objectWithKeyValues:bigDic];
    
    return model;
}


//解析餐品信息数组
+ (NSMutableArray *)resolvingCanPinMsgDataWithDic:(NSDictionary *)dic
{
    NSArray *goodsList = dic[@"goodsList"];
    NSMutableArray *tempArr = [NSMutableArray array];
    for (NSDictionary *innerDic in goodsList) {
        WSOrderCanPinMsgModel *model = [WSOrderCanPinMsgModel mj_objectWithKeyValues:innerDic];;
        [tempArr addObject:model];
    }
    
    //单独 插入优惠券、配送费
    //    @property (nonatomic, copy) NSString *title;
    //    /**2.价格*/
    //    @property (nonatomic, strong) NSNumber *price;
    
    NSNumber *youhuiquanPrice = dic[@"couponAmount"];
    //国际化
    NSString *yhqStr = NSLocalizedString(Coupon, @"优惠券");
    NSString *psfStr = NSLocalizedString(OrderDistribution, @"配送费");
    
    NSDictionary *youhuiquanDic = @{@"title":yhqStr,@"price":youhuiquanPrice};
    WSOrderCanPinMsgModel *youhuiquanModel = [WSOrderCanPinMsgModel mj_objectWithKeyValues:youhuiquanDic];
    
    NSNumber *freight = dic[@"freightAmount"];
    NSDictionary *freightDic = @{@"title":psfStr ,@"price":freight};
    WSOrderCanPinMsgModel *freightModel = [WSOrderCanPinMsgModel mj_objectWithKeyValues:freightDic];
    NSArray *youhuiquanAndFreightArr = @[youhuiquanModel,freightModel];
    
    
    //装入数组中
    [tempArr addObjectsFromArray:youhuiquanAndFreightArr];

    return tempArr;
}

//解析订单信息数组
+ (NSMutableArray *)resolvingOrderMsgDataWithDic:(NSDictionary *)dic
{
    
    //    @property (nonatomic, copy) NSString *title;
    //    /**2.内容*/
    //    @property (nonatomic, copy) NSString *content;
    NSDictionary *userShippingAddressDic = dic[@"userShippingAddress"];
    NSString *name = userShippingAddressDic[@"consignee"];//1.姓名
    
    //2019-2-14可能为空导致奔溃
    name = name ?:@"";
    
    NSString *phone = userShippingAddressDic[@"phone"];//2.电话
    //2019-2-14可能为空导致奔溃
    phone = phone ? : @""; //有值就是自己没有就是@""
    
    NSString *address = userShippingAddressDic[@"detail"]; //3.地址
    address = address?:@"";
    
    NSString *remark = dic[@"remark"]; //4.备注
    NSNumber *payment = dic[@"paymentType"]; //5.支付方式
    NSString *paymentStr = NSLocalizedString(L_Pay_Online, @"在线支付");;
    if (![payment isEqualToNumber:@(0)]) { //!一下
        paymentStr = NSLocalizedString(L_Pay_Offline_Cash, @"线下支付");
    }
    
    NSString *orderId = dic[@"id"]; //6.订单号
    NSString *newAddjiFen = dic[@"bonusAmount"]; //7.新增积分
    
    //国际化
    NSString *xmStr = [NSString stringWithFormat:@"%@:",NSLocalizedString(Name, @"姓名：")];
    NSString *lxdhStr = [NSString stringWithFormat:@"%@:",NSLocalizedString(Order_ContactPhone, @"联系电话：")];
    NSString *dzStr = [NSString stringWithFormat:@"%@:",NSLocalizedString(Address, @"地址：")];
    NSString *bzStr = [NSString stringWithFormat:@"%@:",NSLocalizedString(Order_Remark, @"备注：")];
    NSString *zffsStr = [NSString stringWithFormat:@"%@:",NSLocalizedString(Order_PayType, @"支付方式：")];
    NSString *ddhStr = [NSString stringWithFormat:@"%@:",NSLocalizedString(OrderNumber, @"订单号：")];
    NSString *xzjfStr = [NSString stringWithFormat:@"%@:",NSLocalizedString(XinZhengJiFen, @"新增积分：")];


    //
    NSString *ddxxStr = NSLocalizedString(OrderMessage, @"订单信息");
    NSArray *arr = @[@{@"title":ddxxStr,@"content":@""},
                     @{@"title":xmStr,@"content":name},
                     @{@"title":lxdhStr,@"content":phone},
                     @{@"title":dzStr,@"content":address},
                     @{@"title":bzStr,@"content":remark},
                     @{@"title":zffsStr,@"content":paymentStr},
                     @{@"title":ddhStr,@"content":orderId},
                     @{@"title":xzjfStr,@"content":newAddjiFen},
                     ];
    //转模型
    NSMutableArray *newArrM = [NSMutableArray array];
    
    for (NSDictionary *tempDic in arr) {
        WSOrderMsgModel *model = [WSOrderMsgModel mj_objectWithKeyValues:tempDic];
        [newArrM addObject:model];
    }
    return newArrM;
}




/**解析talbeView用到的数据*/
+ (NSArray *)xinResolvingTableViewDataWithDic:(NSDictionary *)dic {

    if (dic == nil) {
        return @[];
    }
    
    //取 "goodsList" 这个字段里面的值
    NSArray *goodsListArr = dic[@"goodsList"];
    
    NSMutableArray *tempArrM = [NSMutableArray array];
    for (NSDictionary *innerDic in goodsListArr) {
        WSOrderSectionModel *smodel = [WSOrderSectionModel mj_objectWithKeyValues:innerDic];
        [tempArrM addObject:smodel];
    }
    
    //develope log 2019-1-9
    //商品信息,什么没有就一个标题 @“餐品信息” 国际请在这做添加到数组的头部
    WSOrderSectionModel *shangpingMsgModel = [[WSOrderSectionModel alloc] init];
    shangpingMsgModel.title = NSLocalizedString(Home_CanPinMsg, @"餐品信息"); //在这里做国际化
    [tempArrM insertObject:shangpingMsgModel atIndex:0];
    
    return [tempArrM copy];
}

@end
